package win.larryzeal.thymeleaf.config;

import org.springframework.context.MessageSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.thymeleaf.spring4.SpringTemplateEngine;
import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring4.view.ThymeleafViewResolver;
import org.thymeleaf.templatemode.TemplateMode;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

/**
 * Created by 张少昆 on 2017/10/20.
 */
@Configuration
@EnableWebMvc
@ComponentScan( basePackages = "win.larryzeal.thymeleaf",
		includeFilters = @ComponentScan.Filter( type = FilterType.ANNOTATION, classes = Controller.class ) )
public class MvcConfig extends WebMvcConfigurerAdapter {

	/**
	 * SpringMVC 处理上传文件的信息
	 *
	 * @return 文件上传解析器
	 */
	@Bean
	public CommonsMultipartResolver multipartResolver() throws IOException{
		CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
		multipartResolver.setDefaultEncoding(StandardCharsets.UTF_8.displayName());
		multipartResolver.setUploadTempDir(new FileSystemResource(System.getProperty("java.io.tmpdir")));
		return multipartResolver;
	}

	@Bean
	public ThymeleafViewResolver viewResolver(){
		ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
		viewResolver.setTemplateEngine(templateEngine());
		viewResolver.setCharacterEncoding("utf-8");
		return viewResolver;
	}

	// ref: org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties
	@Bean
	public SpringResourceTemplateResolver templateResolver(){
		SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();

		resolver.setPrefix("classpath:/views/"); //"/WEB-INF/VIEWS/"
		resolver.setSuffix(".html");

		resolver.setTemplateMode(TemplateMode.HTML);

		resolver.setCacheable(false);
		resolver.setCharacterEncoding("utf-8");
		resolver.setOrder(1);

		return resolver;
	}

	@Bean
	public SpringTemplateEngine templateEngine(){
		SpringTemplateEngine templateEngine = new SpringTemplateEngine();
		templateEngine.setTemplateResolver(templateResolver());
		return templateEngine;
	}

	@Bean
	public MessageSource messageSource(){
		ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
		messageSource.addBasenames("index"); //需要指定国际化消息文件名字的前缀，如demo.properties/demo_en.properties
		messageSource.setDefaultEncoding("UTF-8");
		messageSource.setFallbackToSystemLocale(false);
		return messageSource;
	}

	@Bean
	public LocaleResolver localeResolver(){
		CookieLocaleResolver cookieLocaleResolver = new CookieLocaleResolver();
		return cookieLocaleResolver;
	}

}
